import ButtonGroupEdit from '@/components/common/ButtonGroupEdit/ButtonGroupEdit.vue'
import { COLUMN_WIDTH, createCommonPage } from '@/utils/service.js'
import { dateFormat } from '@/utils/date'
// import { Debounce } from '/src/utils/uitls'

export const createForm = () => ({
  k: '',
  desc: true,
  // orderField: 'lastProduceTime',
  ...createCommonPage(),
})

/* {
      "organId": 1,
      "organName": "北京网信办",
      "createTime": "2024-08-09 00:00:00",
      "createAccount": "admin",
      "updateAccount": null,
      "updateTime": null,
      "produceNum": 10,
      "lastProduceTime": "2024-08-27 12:19:08"
  } */
export const createColumns = (vm) => [
  {
    title: '序号',
    key: 'id',
    align: 'center',
    width: 80,
    render(h, params) {
      return <div>{(vm.formData.pageNo - 1) * vm.formData.pageSize + params.index + 1}</div>
    },
  },
  {
    title: '供稿单位',
    align: 'center',
    render(h, params) {
      if (params.row.editable) {
        return (
          <i-input
            style="width: 200px"
            value={params.row.organName_bk}
            on-input={(val) => (params.row.organName_bk = val)}
          ></i-input>
        )
      } else {
        return h('span', params.row.organName)
      }
    },
  },
  {
    title: '配置日期',
    align: 'center',
    // width: COLUMN_WIDTH + 50,
    render(h, params) {
      if (!params.row.createTime) return h('span', '-')
      return h('span', dateFormat(params.row.createTime, 'YYYY.MM.DD HH:mm:ss'))
    },
  },
  {
    title: '供稿日期',
    align: 'center',
    // width: COLUMN_WIDTH + 50,
    render(h, params) {
      if (!params.row.lastProduceTime) return h('span', '-')
      return h('span', dateFormat(params.row.lastProduceTime, 'YYYY.MM.DD HH:mm:ss'))
    },
  },
  {
    title: '供稿额度',
    align: 'center',
    render(h, params) {
      if (params.row.editable) {
        return (
          <input-number
            value={params.row.produceNum_bk}
            on-input={function (value) {
              params.row.produceNum_bk = value
            }}
            formatter={function (val) {
              if (val <= 0) {
                params.row.produceNum_bk = 0
                return 0
              }
              const maxLength = 10 // 设定最大长度
              const str = String(val)
              let res = ''
              if (str.length > maxLength) {
                res = str.slice(0, maxLength) // 截断超过长度的部分
              } else {
                res = str
              }
              params.row.produceNum_bk = Number(res)
              return Number(res)
            }}
            precision={0}
            min={0}
            size="large"
            style="width: 120px"
            placeholder="请输入供稿额度"
          ></input-number>
        )
      } else {
        return h('span', params.row.produceNum)
      }
    },
  },
  {
    title: '操作',
    align: 'center',
    render(h, params) {
      return (
        <div>
          <ButtonGroupEdit
            edit={params.row.editable}
            on-change={(val) => (params.row.editable = val)}
            on-on-cancel={() => vm.handleEditTableRowCancel(params.row)}
            on-on-complete={() => vm.handleEditTableRow(params.row)}
          ></ButtonGroupEdit>

          <poptip confirm transfer title="确定删除?" on-on-ok={() => vm.handleDeleteTableRow(params.row)}>
            <icon-button
              icon="delete_active"
              title="删除"
              class="ml14"
              width="26"
              height="26"
              style="vertical-align: sub"
              activated={false}
            ></icon-button>
          </poptip>
        </div>
      )
    },
  },
]
